package com.zsc.wxapp.controller.admin;

import com.github.pagehelper.PageInfo;
import com.zsc.wxapp.context.BaseContext;
import com.zsc.wxapp.entity.dto.AddEndUserDTO;
import com.zsc.wxapp.entity.dto.AddManagerDTO;
import com.zsc.wxapp.entity.dto.ModifyInfoDTO;
import com.zsc.wxapp.entity.dto.SetStatusDTO;
import com.zsc.wxapp.entity.vo.CustomerOfManagerVO;
import com.zsc.wxapp.entity.vo.UserInfoVO;
import com.zsc.wxapp.result.Result;
import com.zsc.wxapp.services.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController("adminUserController")
@RequestMapping("/api/wxapp/admin")
@Api(tags = "管理员用户相关接口")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/getAdminInfo")
    @ApiOperation("获取管理员账号相关数据信息")
    public Result<UserInfoVO> getAdminInfo() {
        //获取当前用户的id
        Long userId = BaseContext.getCurrentId();
        UserInfoVO result = userService.getUserInfo(userId);
        return Result.success(result);
    }

    /**
     * 获取经理名称
     *
     * @return
     */
    @GetMapping("/getManagerName")
    @ApiOperation("获取经理名称， 传1为获取已经注册的客户经理，传0为获取所有经理名称")
    public Result<List<String>> getManagerName(Integer type) {
        log.info("获取客户经理账号");
        List<String> result = userService.getManagerName(type);
        return Result.success(result);
    }

    /**
     * 添加客户经理
     * @param addManagerDTO
     * @return
     */
    @PostMapping("/addManager")
    @ApiOperation("添加客户经理")
    public Result addManager(@RequestBody AddManagerDTO addManagerDTO) {
        try {
            userService.addManager(addManagerDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    /**
     * 获取客户经理所负责的用户
     * @param managerName
     * @return
     */
    @GetMapping("/getEndUserByManager")
    @ApiOperation("获取客户经理所负责的用户")
    public Result<List<CustomerOfManagerVO>> getEndUserByManager(String managerName) {
        List<CustomerOfManagerVO> result = userService.getEndUserByManager(managerName);
        return Result.success(result);
    }

    /**
     * 添加零售用户
     * @param addEndUserDTO
     * @return
     */
    @PostMapping("/addEndUser")
    @ApiOperation("添加零售用户")
    public Result addEndUser(@RequestBody AddEndUserDTO addEndUserDTO) {
        log.info("添加零售用户：addEndUserDTO：{}", addEndUserDTO);
        try {
            userService.addEndUser(addEndUserDTO);
        } catch (Exception e) {
            return Result.error(e.getMessage());
        }
        return Result.success();
    }

    /**
     * 获取所有用户
     * @param pageSize
     * @param pageNum
     * @param type
     * @return
     */
    @GetMapping("/getAllUser")
    @ApiOperation("获取所有用户, 传0为零售用户, 传1为客户经理")
    public Result<PageInfo> getAllUser(Integer pageSize, Integer pageNum, Integer type) {
        return Result.success(userService.getAllUser(pageSize, pageNum, type));
    }

    /**
     * 设置用户状态
     * @param setStatusDTO
     * @return
     */
    @PutMapping("/setStatus")
    @ApiOperation("设置用户状态")
    public Result setStatus(@RequestBody SetStatusDTO setStatusDTO) {
        userService.setStatus(setStatusDTO);
        return Result.success();
    }

    /**
     * 修改密码
     * @param modifyInfoDTO
     * @return
     */
    @PutMapping("/modifyPassword")
    @ApiOperation("修改密码")
    public Result modifyPassword(@RequestBody ModifyInfoDTO modifyInfoDTO) {
        userService.modifyPassword(modifyInfoDTO);
        return Result.success();
    }

    /**
     * 修改手机号
     * @param modifyInfoDTO
     * @return
     */
    @PutMapping("/modifyPhone")
    @ApiOperation("修改手机号")
    public Result modifyPhone(@RequestBody ModifyInfoDTO modifyInfoDTO) {
        log.info("修改手机号：modifyInfoDTO：{}", modifyInfoDTO);
        userService.modifyPhone(modifyInfoDTO);
        return Result.success();
    }

}
